package com.example.eat.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.eat.R;
import com.example.eat.adapter.RecipeAdapter;
import com.example.eat.dao.RecipeDao;
import com.example.eat.model.Recipe;

import java.util.ArrayList;
import java.util.List;

public class FavoritesFragment extends Fragment implements RecipeAdapter.OnItemClickListener {
    private static final String ARG_USER_ID = "userId";
    private int userId;
    private RecyclerView recyclerView;
    private RecipeAdapter recipeAdapter;
    private List<Recipe> recipeList;
    private RecipeDao recipeDao;

    public FavoritesFragment() {
        // Required empty public constructor
    }

    public static FavoritesFragment newInstance(int userId) {
        FavoritesFragment fragment = new FavoritesFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_USER_ID, userId);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            userId = getArguments().getInt(ARG_USER_ID);
        }
        recipeDao = new RecipeDao(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_favorites, container, false);

        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recipeList = new ArrayList<>();
        recipeAdapter = new RecipeAdapter(getContext(), recipeList, userId);
        recipeAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(recipeAdapter);

        loadFavorites();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        loadFavorites();
    }

    private void loadFavorites() {
        // 获取用户的所有收藏食谱
        recipeList = recipeDao.getFavoriteRecipes(userId);
        recipeAdapter.setRecipes(recipeList);
        recipeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int position) {
        Recipe recipe = recipeList.get(position);
        // 跳转到食谱详情页
    }

    @Override
    public void onEditClick(int position) {
        // 编辑收藏的食谱
    }

    @Override
    public void onDeleteClick(int position) {
        // 删除收藏的食谱
    }

    @Override
    public void onFavoriteClick(int position) {
        Recipe recipe = recipeList.get(position);

        // 移除收藏
        if (recipeDao.removeFavorite(userId, recipe.getId())) {
            recipeList.remove(position);
            recipeAdapter.notifyItemRemoved(position);
            Toast.makeText(getContext(), "已取消收藏", Toast.LENGTH_SHORT).show();
        }
    }
}